home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: thinkage.on.ca!atbowler
- From: atbowler@thinkage.on.ca (Alan Bowler)
- Subject: Re: How to get a random strin
- Message-ID: <Dn5E0J.GKL@thinkage.on.ca>
- Sender: news@thinkage.on.ca
- Organization: Thinkage Ltd.
- References: <4g19id$p7n@gail.ripco.com>
- Date: Wed, 21 Feb 1996 22:48:19 GMT
-
- In article <4g19id$p7n@gail.ripco.com> mambuhl@ripco.com (Martin Ambuhl) writes:
- >chancl@nevada.edu (Clapton Chan) in <4fh5od$qq0@news.nevada.edu> asks:
- >
- >[A poor practice follows]
- >if you do not #include <time.h>, you need
- > srand((unsigned)time(NULL));
- >
-
- Worse than poor. time() returns a time_t which might not be an
- integer type. The above code implicitly declares "time()" as returning
- int. Adding the cast to unsigned will not change that fact that
- this implicit declaration means your code could be looking in the
- wrong place. For example: Suppose "time_t" is actually "double"
- and not "int", and that the machine uses has separate floating
- point and integer registers. (x86, pdp-11, ibm/370). Then the
- above code could well result in the same vale being passed to srand()
- every time the program is called, because time() set the floating
- point result register and the above code is looking at an integer
- result register which untouched by time().
-
- Aside:
- Chosing double for time_t and clock_t ss likely to become a more
- common choice of implementors in the future. It simplifies a lot of
- code.
-